<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: jz
 * Date: 16/7/19
 * Time: 下午9:46
 */

class Admin extends MY_Controller {
    public function index()
    {
        $data = array(
            'user_info' => array(
                'email' => $this->session->userdata('email')
            )
        );
    	$this->load->view('admin/index.html', $data); 
    }
    public function copy()
    {
        $data = array (
            'server_info' => array(
                '操作系统' => PHP_OS,
                '运行环境' => $this->input->server('SERVER_SOFTWARE'),
                'PHP运行方式' => php_sapi_name(),
                'CI版本' => CI_VERSION,
                '上传附件限制' => ini_get('upload_max_filesize'),
                '执行时间限制' => ini_get('max_execution_time') . '秒'
             ),
            'user_info' => array(
                'email' => $this->session->userdata('email'),
                'ip' => $this->input->ip_address(),
                'login_time' =>  date('Y-m-d', $this->session->userdata('login_time'))
            ),
            'php_version' => PHP_VERSION,
            'server_ip' => gethostbyname($this->input->server('SERVER_NAME'))
        );
        
        $this->load->view('admin/copy.html', $data);
    }
    public function password()
    {
        $data = array(
            'email' => $this->session->userdata('email')
        );
            
        $this->load->view('admin/change_password.html', $data);
    }
    
    public function change()
    {
        $email = $this->input->post('email');
        $old_pass = $this->input->post('old_pass');
        $new_pass = $this->input->post('new_pass');
        if ($email != $this->session->userdata('email')){
            $data = array(
                'code' => 'error_email',
                'msg' => '登录用户只能修改自己的用户密码'
            );
            $json = json_encode($data);
            echo $json;
            return;
        }
        $this->load->model('admin/admin_model');
        $ret = $this->admin_model->change($email, $old_pass, $new_pass);
        if (!$ret){
            $data = array(
                'code' => 'error_user_password',
                'msg' => '用户名或密码错'
            );
        }else {
            $data = array(
                'code' => 'ok',
                'msg' => '修改成功',
                'href' => './admin/admin/copy.html'
            );
        }
        $json = json_encode($data);
        echo $json;
    }
    
}